Ext.ns("App");

App.ChangePasswordDialog = function() {

    return {

        getForm: function() {
            var frm = new Ext.form.FormPanel({
                api: {
                    submit: UserAction.changePassword
                },
                labelAlign: 'left',
                buttonAlign: 'center',
                bodyStyle: 'padding:5px',
                frame: true,
                labelWidth: 80,
                defaultType: 'textfield',
                defaults: {
                    inputType: 'password',
                    allowBlank: false,
                    anchor: '90%',
                    enableKeyEvents: true
                },
                items: [{
                    name: 'oldPassword',
                    fieldLabel: '旧密码',
                    listeners: {
                        scope: this,
                        keypress: function(field, e) {
                            if (e.getKey() == 13) {
                                var obj = this.frm.form.findField("newPassword");
                                if (obj) obj.focus();
                            }
                        } //keypress
                    }
                },
                {
                    name: 'newPassword',
                    fieldLabel: '新密码',
                    id: "newPassword",
                    listeners: {
                        scope: this,
                        keypress: function(field, e) {
                            if (e.getKey() == 13) {
                                var obj = this.frm.form.findField("confirmPassword");
                                if (obj) obj.focus();
                            }
                        } //keypress
                    }
                },
                {
                    name: 'confirmPassword',
                    fieldLabel: '确认密码',
                    vtype: 'password',
                    initialPassField: 'newPassword',
                    listeners: {
                        scope: this,
                        keypress: function(field, e) {
                            if (e.getKey() == 13) {
                                this.submit();
                            }
                        } //keypress
                    }
                }],
                //items
                buttons: [{
                    text: '确定',
                    scope: this,
                    handler: function() {
                        this.submit();
                    }
                },
                {
                    text: '重置',
                    scope: this,
                    handler: function() {
                        this.frm.form.reset()
                    }
                }] //buttons
            }); //FormPanel
            return frm;
        },

        getDialog: function() {
            this.frm = this.getForm();
            var dlg = new Ext.Window({
                width: 400,
                height: 300,
                title: '修改密码',
                plain: true,
                closable: true,
                resizable: false,
                frame: true,
                layout: 'fit',
                closeAction: 'hide',
                border: false,
                modal: true,
                items: [this.frm]
            }); //dlg
            return dlg;
        },
        //getDialog

        submit: function() {
            if (this.frm.form.isValid()) {
                this.frm.form.doAction("directsubmit", {
                    waitTitle: '保存数据',
                    waitMsg: '正在保存……',
                    scope: this,
                    method: 'post',
                    params: "",
                    success: function(form, action) {
                        var msg = "";
                        if (action.result.success) {
                            msg = "密码已更新。";
                            this.dlg.hide();
                        }
                        Ext.Msg.alert('信息', msg);
                    },
                    failure: function(form, action) {
                        if (action.result) {
                            if (action.result.msg) Ext.Msg.alert('错误', action.result.msg);
                        }
                        if (action.message) Ext.Msg.alert('错误', action.message);
                    }
                });
            }
        },

        show: function() {
            if (!this.dlg) {
                this.dlg = this.getDialog();
            }
            this.dlg.show();
        }

    }; //return
} (); //function
